package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.common.entity.CommentDetailEntity;
import com.JZ.QM.common.entity.UserCommentEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.dto.CollectAndCommentDto;
import com.JZ.QM.feign.CombinationsFeignService;
import com.JZ.QM.service.UserCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.activation.CommandMap;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/6 0:46
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/userComment")
public class UserCommentController {
    @Resource
    CombinationsFeignService combinationsFeignService;
    @Resource
    UserCommentService userCommentService;

    /**
     * 评论组合产品
     * @return
     */
    @RequestMapping("/commentCombination")
    public CommonResult commentCombination(@Valid CollectAndCommentDto collectAndCommentDto){
        //组合基本信息
        LinkedHashMap combinationBaseInfoEntity=new LinkedHashMap();
        UserCommentEntity userCommentEntity=new UserCommentEntity();
        boolean commentState=false;

        //通过组合id获取组合的基本信息
        combinationBaseInfoEntity= (LinkedHashMap) combinationsFeignService.getComBaseInfo(collectAndCommentDto.getComBaseId()).get("combination");
        if (combinationBaseInfoEntity.isEmpty()){
            return CommonResult.error(201,"该组合不存在");
        } else {
            //创建userComment实体用于插入数据
            userCommentEntity.setComBaseId(collectAndCommentDto.getComBaseId());
            userCommentEntity.setCommentContent(collectAndCommentDto.getCommentContent());
            userCommentEntity.setUserId(collectAndCommentDto.getUserId());

            //插入评论数据
            commentState= userCommentService.save(userCommentEntity);
            if(commentState){
                return CommonResult.success("评论成功").put("commentContent",userCommentEntity.getCommentContent());
            } else {
                return CommonResult.error(201,"评论创建失败");
            }
        }
    }

    /**
     * 根据组合id获取素有的评论信息
     * @param comBaseId 组合id
     * @return
     */
    @RequestMapping("/showAllComments")
    public CommonResult showComments(Long comBaseId){
        //消息的详细信息列表
        ArrayList<CommentDetailEntity> commentDetailEntities=new ArrayList<>();

        //通过组合id获取所有的组合评论信息
        commentDetailEntities=userCommentService.getAllCommentDetails(comBaseId);
        return CommonResult.success().put("allComments",commentDetailEntities.toArray());


    }
}
